#ifndef __PROPERTIES_MODEL_H__
#define __PROPERTIES_MODEL_H__ 1

#include <QAbstractItemModel>
#include <QStringList>
#include <QModelIndex>

#include "client_connection.h"
#include "properties_item.h"

class CPropertiesModel : public QAbstractItemModel
{
	Q_OBJECT
	
public:
	CPropertiesModel(QObject* pParent = NULL);
	virtual ~CPropertiesModel();

	QModelIndex insertSection(const QVariant& pSectionName);
	QModelIndex insertKey(const QModelIndex& pIndex, const QVariant& pKey, 
			const QVariant& pValue);

	bool loadProperties(const QString& pFilename);
	bool saveProperties(const QString& pFilename);
	void sendConfig(CClientConnection* pConnection);

	void dropProperties();

	bool parentIsRoot(const QModelIndex& pIndex) const;
	inline void setReadOnly(bool pReadOnly) { mReadOnly = pReadOnly; }
	inline bool isReadOnly() const { return mReadOnly; }

signals:
	void modelSorted();

private slots:
	void configReceived(const QString& pSection, const QString& pKey, const QString& pValue);

private:
	CPropertiesModel(const CPropertiesModel& );
	CPropertiesModel& operator=(const CPropertiesModel& );

	virtual QModelIndex index(int pRow, int pColumn, const QModelIndex& pParent = QModelIndex()) const;
	virtual QModelIndex parent(const QModelIndex& pIndex) const;
	virtual int rowCount(const QModelIndex& pParent) const;
	virtual int columnCount(const QModelIndex& pParent) const;
	virtual QVariant data(const QModelIndex& pIndex, int pRole) const;
	bool setData(const QModelIndex &pIndex, const QVariant &pValue, int role = Qt::EditRole);
	virtual Qt::ItemFlags flags(const QModelIndex& pIndex) const;
	QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
	void sort(int pColumn, Qt::SortOrder pOrder);

	bool insertRows(int pPosition, int pRows, 
			const QModelIndex &pParent = QModelIndex());
	bool removeRows(int position, int rows, 
			const QModelIndex &parent = QModelIndex());

	CPropertiesItem* getItem(const QModelIndex& pIndex) const;

	bool mLoaded;
	bool mReadOnly;
	CPropertiesItem* mRootItem;
};

#endif /* __PROPERTIES_MODEL_H__ */

